<?php

namespace app\admin\controller;

use app\admin\services\CityService;
use app\admin\services\QuanGuanMemberService;
use app\admin\services\StoreService;
use think\App;

class StoreController extends Common implements ICurd
{
    /**
     * 馆长列表
     */
    private $storeMemberList;

    /**
     * 省份列表
     */
    private $provinceList;

    /**
     * 城市列表
     */
    private $cityList;

    /**
     * 区域列表
     */
    private $areaList;

    public function initialize()
    {
        parent::initialize();
        $this->setStoreMemberList();
        $this->setProvinceList();
        $this->setCityList();
        $this->setAreaList();
    }

    public function index()
    {
        // TODO: Implement index() method.
        if (request()->isPost()) {
            $requestData = input('post.');
            $param['page'] = $requestData['page'] ?: 1;
            $param['pageSize'] = $requestData['limit'] ?: 10;
            $param['key'] = trim($requestData['key']) ?: "";
            $param['member_id'] = $requestData['member_id'] ?: 0;
            // 需要查询的字段
            $field = "
                id,
                title,
                status,
                member_id,
                create_time,
                link_name,
                link_mobile,
                province_id,
                city_id,
                area_id
            ";
            $provinceList = array_column($this->provinceList, null, 'id');
            $cityList = array_column($this->cityList, null, 'id');
            $areaList = array_column($this->areaList, null, 'id');
            $list = $this->getService()->list($param, $field);
            foreach ($list['data'] as &$item) {
                $item['create_time'] = date("Y-m-d H:i:s", $item['create_time']);
                $item['member_id'] = $this->storeMemberList[$item['member_id']]['name'];
                $item['province_id'] = $provinceList[$item['province_id']]['title'];
                $item['city_id'] = $cityList[$item['city_id']]['title'];
                $item['area_id'] = $areaList[$item['area_id']]['title'];
            }
            return $this->jsonList($list['data'], $list['total']);
        }
        return $this->fetch("store/index");
    }

    public function create()
    {
        // TODO: Implement create() method.
        if (request()->isPost()) {
            $param = $this->mapping();
            $res = $this->getService()->save($param);
            if ($res) {
                return $this->jsonSuccess("index");
            }
            return $this->jsonError();
        }
        return $this->fetch("store/create");
    }

    public function update($id = 0)
    {
        // TODO: Implement update() method.
        if (request()->isPost()) {
            $param = $this->mapping();
            $res = $this->getService()->save($param, $id);
            if ($res) {
                return $this->jsonSuccess("index");
            }
            return $this->jsonError();
        } else {
            $info = $this->getService()->getInfoById($id);
            $descImgs = [];
            if( !empty($info['desc_imgs']) ){
                $descImgs = json_decode($info['desc_imgs'], true);
            }
            foreach ($descImgs as &$img) {
                $img = env('DOMAIN_PATH') . $img;
            }
            $info['desc_imgs'] = $descImgs;
            $this->assign([
                'info' => json_encode($info, true),
                'infoArr' => $info,
            ]);
            return $this->fetch("store/update");
        }
        return $this->jsonError();
    }

    public function remove()
    {
        // TODO: Implement remove() method.
        if (request()->isPost()) {
            $data = input('post.');
            $id = (int)$data['id'];
            $res = $this->getService()->save(['is_delete' => 2], $id);
            if ($res) {
                return $this->jsonSuccess("index");
            }
            return $this->jsonError();
        }
        return $this->jsonError();
    }

    public function detail()
    {
        // TODO: Implement detail() method.
    }

    public function setStatus()
    {
        // TODO: Implement setStatus() method.
        if (request()->isPost()) {
            $id = input('post.id', 0, 'int');
            $status = input('post.status', 0, 'int');
            $res = $this->getService()->save([
                'status' => $status
            ], $id);
            if ($res) {
                return $this->jsonSuccess('index');
            }
            return $this->jsonError();
        }
        return $this->jsonError();
    }

    private function getService()
    {
        return StoreService::makeInstance();
    }

    private function mapping()
    {
        $data = input('post.');
        $param = [
            'title' => trim($data['title']) ?: "",
            'status' => (int)$data['status'] ?: 1,
            'sort' => (int)$data['sort'] ?: 0,
            'member_id' => (int)$data['member_id'] ?: 0,
            'link_mobile' => trim($data['link_mobile']) ?: "",
            'link_name' => trim($data['link_name']) ?: "",
            'province_id' => (int)$data['province_id'] ?: 0,
            'city_id' => (int)$data['city_id'] ?: 0,
            'area_id' => (int)$data['area_id'] ?: 0,
            'address' => trim($data['address']) ?: "",
            'desc' => trim($data['desc']) ?: "",
        ];
        return $param;
    }

    private function setStoreMemberList()
    {
        $memberService = new QuanGuanMemberService();
        $this->storeMemberList = $memberService->getOpenDataByIden("id,name", 4);
        $this->assign([
            'storeMemberList' => $this->storeMemberList
        ]);
    }

    /**
     * 获取省份列表
     */
    private function setProvinceList()
    {
        $cityService = new CityService();
        $list = $cityService->getListByLevel(1);
        $this->provinceList = $list->toArray();
        $this->assign([
            'provinceList' => $this->provinceList
        ]);
    }

    /**
     * 获取城市列表
     */
    private function setCityList()
    {
        $cityService = new CityService();
        $list = $cityService->getListByLevel(2);
        $this->cityList = $list->toArray();
        $this->assign([
            'cityList' => $this->cityList
        ]);
    }

    /**
     * 获取区域列表
     */
    private function setAreaList()
    {
        $cityService = new CityService();
        $list = $cityService->getListByLevel(3);
        $this->areaList = $list->toArray();
        $this->assign([
            'areaList' => $this->areaList
        ]);
    }

}